/** 
 * MatchValidator.java Created by iweerarathna on Sep 1, 2011.
 * 
 * Copyright (c) 2011 Virtusa Corporation. All Rights Reserved.<br><br>. 
 * 
 */
package org.uim.cricket.analyzer.validate;

import org.uim.cricket.entities.MatchPlayer;
import org.uim.cricket.entities.match.CricketMatch;
import java.util.*;

/**
 * TODO Class description goes here.
 * @version 
 * @author iweerarathna
 */
public class MatchValidator {

	public static String VALIDATOR_BEAN_FILE = "../config/validator-beans.xml";
	public static String DEFAULT_VALIDATOR_LIST_BEAN_ID = "validating-List";
	
    private CricketMatch match = null;
    private String beanID = null;
    private Map<String, List<MatchPlayer>> players = null;
    
    public MatchValidator(CricketMatch theMatch, Map<String, List<MatchPlayer>> players, String validatingBeanID) {
        this.match = theMatch;
        this.players = players;
        this.beanID = validatingBeanID;
    }
    
    public void doValidation() throws Exception {
    	if (this.match == null) {
    		throw new IllegalArgumentException("CricketMatch object can't be EMPTY!");
    	}
    	ValidatorList vlist = null;
    	
    	VLogger.printHeader(" - Starting new validator for match: " +
    			match.getId().getPrefix() + " # " + match.getId().getInternationalID() );
    	
    	VLogger.debug("    fetching beans...");
    	FetchValidatingBeans beans = new FetchValidatingBeans(VALIDATOR_BEAN_FILE);
        beans.loadBeans();
        
        if (beanID == null || beanID.length() == 0) {
        	VLogger.debug("     using default bean. [" + DEFAULT_VALIDATOR_LIST_BEAN_ID + "]");
        	vlist = (ValidatorList) beans.fetchBean(DEFAULT_VALIDATOR_LIST_BEAN_ID);
        } else {
        	VLogger.debug("     - using bean. [" + this.beanID + "]");
        	vlist = (ValidatorList) beans.fetchBean(beanID);
        }
        
        VLogger.debug("    ok.");
        
        if (vlist == null || vlist.isEmpty()) {
        	VLogger.printSubHeader(" - Completed validating successfully!");
        	return;
        }
        
        while (!vlist.isEmpty()) {
        	ValidatableMatch validator = vlist.popAnalyzer();
        	if (validator == null) continue;
        	
	        VLogger.debug("      # executing [" + validator.getClass().getName() + "]...");
	        validator.processValidation(match, players);
	        VLogger.debug("         - ok.");
        }
        
        VLogger.printSubHeader(" - Completed validating successfully!");
    }
    
    public void setMatch(CricketMatch match) {
        this.match = match;
    }
    
    public CricketMatch getMatch() {
        return match;
    }
    
    public String getBeanID() {
		return beanID;
	}
    
    public void setBeanID(String beanID) {
		this.beanID = beanID;
	}
    
}
